/**
 * Filename: SwitchMusicActivity.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/5/7 22:28 
 */
package com.github.postsanf.yinian.activity;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.OvershootInterpolator;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.SwitchMusicAdapter;
import com.github.postsanf.yinian.bean.YNMusic;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.github.postsanf.yinian.utils.WrapContentLinearLayoutManager;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;

public class SwitchMusicActivity extends BaseActivity implements ISkinUpdate {
    private Toolbar     mToolbar;
    private RecyclerView rv_switch_music;
    private WrapContentLinearLayoutManager mLinearLayoutManager;
    private List<YNMusic> mMusic = new ArrayList<>();
    private SwitchMusicAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_switch_music);
        BaseApplication.getInstance().mActivitys.add(this);
        initView();
    }

    private void  initView() {
        mToolbar = (Toolbar) findViewById(R.id.switch_music_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);

        rv_switch_music = (RecyclerView) findViewById(R.id.rv_switch_music);

        mLinearLayoutManager = new WrapContentLinearLayoutManager(this);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_switch_music.setLayoutManager(mLinearLayoutManager);

        mAdapter   = new SwitchMusicAdapter(this, mMusic);

        rv_switch_music.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));
        rv_switch_music.getItemAnimator().setAddDuration(300);
        rv_switch_music.getItemAnimator().setRemoveDuration(300);
        rv_switch_music.getItemAnimator().setMoveDuration(300);
        rv_switch_music.getItemAnimator().setChangeDuration(300);
        rv_switch_music.setHasFixedSize(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void updateTheme() {
        if(mToolbar!=null){
            try {
                Resources mResource= SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    // 友盟统计
    public void onResume(){
        super.onResume();
        if(SkinPackageManager.getInstance(this).mResources!=null){
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause(){
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
